
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>轮播图点击切换</title>
  <style>
    * {
      box-sizing: border-box;
    }

    .slider {
      width: 560px;
      height: 400px;
      overflow: hidden;
    }

    .slider-wrapper {
      width: 100%;
      height: 320px;
    }

    .slider-wrapper img {
      width: 100%;
      height: 100%;
      display: block;
    }

    .slider-footer {
      height: 80px;
      background-color: rgb(100, 67, 68);
      padding: 12px 12px 0 12px;
      position: relative;
    }

    .slider-footer .toggle {
      position: absolute;
      right: 0;
      top: 12px;
      display: flex;
    }

    .slider-footer .toggle button {
      margin-right: 12px;
      width: 28px;
      height: 28px;
      appearance: none;
      border: none;
      background: rgba(255, 255, 255, 0.1);
      color: #fff;
      border-radius: 4px;
      cursor: pointer;
    }

    .slider-footer .toggle button:hover {
      background: rgba(255, 255, 255, 0.2);
    }

    .slider-footer p {
      margin: 0;
      color: #fff;
      font-size: 18px;
      margin-bottom: 10px;
    }

    .slider-indicator {
      margin: 0;
      padding: 0;
      list-style: none;
      display: flex;
      align-items: center;
    }

    .slider-indicator li {
      width: 8px;
      height: 8px;
      margin: 4px;
      border-radius: 50%;
      background: #fff;
      opacity: 0.4;
      cursor: pointer;
    }

    .slider-indicator li.active {
      width: 12px;
      height: 12px;
      opacity: 1;
    }
  </style>
</head>

<body>
  <div class="slider">
    <div class="slider-wrapper">
      <img src="./01.jpg" alt="" />
    </div>
    <div class="slider-footer">
      <p>邦邦邦邦？</p>
      <ul class="slider-indicator">
        <li class="active"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
      <div class="toggle">
        <button class="prev">&lt;</button>
        <button class="next">&gt;</button>
      </div>
    </div>
  </div>
  <script>
    const data = [
      { url: './01.jpg', title: '颗秒', color: 'rgb(100, 67, 68)' },
      { url: './02.jpg', title: '难道他真的是赋能哥', color: 'rgb(43, 35, 26)' },
      { url: './03.jpg', title: '别挡我的路', color: 'rgb(36, 31, 33)' },
      { url: './04.jpg', title: '邦邦邦邦', color: 'rgb(139, 98, 66)' },
      { url: './05.jpg', title: '他们走不了了', color: 'rgb(67, 90, 92)' },
      { url: './06.jpg', title: '当心眼睛', color: 'rgb(166, 131, 143)' },
    ]
    const img = document.querySelector('.slider-wrapper img')
    const p = document.querySelector('.slider-footer p')
    const footer = document.querySelector('.slider-footer')
    const next = document.querySelector('.next')
    let i = 0 
    next.addEventListener('click', function () {
      i++
      i = i >= data.length ? 0 : i
      toggle()
    })

    const prev = document.querySelector('.prev')
    prev.addEventListener('click', function () {
      i--
      i = i < 0 ? data.length - 1 : i
      toggle()
    })

    function toggle() {
      img.src = data[i].url
      p.innerHTML = data[i].title
      footer.style.backgroundColor = data[i].color
      document.querySelector('.slider-indicator .active').classList.remove('active')
      document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active')
    }


    let timerId = setInterval(function () {
      next.click()
    }, 1000)


    const slider = document.querySelector('.slider')
    slider.addEventListener('mouseenter', function () {
      clearInterval(timerId)
    })
    slider.addEventListener('mouseleave', function () {
      if (timerId) clearInterval(timerId)
      timerId = setInterval(function () {
        next.click()
      }, 1000)
    })
  </script>
</body>

</html>